package com.zx.ymy.base

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import com.example.basecomponent.TipDialog
import com.uber.autodispose.AutoDispose
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider
import com.zx.yixun.util.RxSchedulers
import com.zx.ymy.net.NetObserver
import io.reactivex.Observable
import java.util.concurrent.TimeUnit

open class BaseFragment : Fragment() {

    protected lateinit var mBaseContext: BaseActivity
    protected var isViewCreate = false
    protected var isShowToUser = false
    protected var isLoaded = false //数据是否加载成功，
    private var successDialog: TipDialog? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is BaseActivity) {
            mBaseContext = context
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        this.isViewCreate = true
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        this.isShowToUser = isVisibleToUser
    }

    protected fun <T> Observable<BaseResponse<T>>.runRx(onSuccess: ((res: T?) -> Unit)?,
                                                        onFailure: ((errorMsg: String?) -> Unit)? = null) {
        this.subscribeOn(RxSchedulers.getScheduler())
                .observeOn(RxSchedulers.ui)
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle)))
                .subscribe(object : NetObserver<T>(null,onSuccess, onFailure) {
                    override fun onFailure(e: Throwable, code: Int?, errorMsg: String?) {
                        super.onFailure(e, code, errorMsg)
                        if (activity is BaseActivity) {
                            (activity as BaseActivity).showError(errorMsg)
                        } else {
                            Log.e("BaseFragment", "the attach activity is not BaseActivity=$activity")
                        }
                    }
                })
    }

    protected fun <T> Observable<BaseResponse<T>>.runRxLoading(onSuccess: ((res: T?) -> Unit)?,
                                                        onFailure: ((errorMsg: String?) -> Unit)? = null) {
        this.subscribeOn(RxSchedulers.getScheduler())
                .observeOn(RxSchedulers.ui)
                .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycle)))
                .subscribe(object : NetObserver<T>(activity, onSuccess, onFailure) {
                    override fun onFailure(e: Throwable, code: Int?, errorMsg: String?) {
                        super.onFailure(e, code, errorMsg)
                        if (activity is BaseActivity) {
                            (activity as BaseActivity).showError(errorMsg)
                        } else {
                            Log.e("BaseFragment", "the attach activity is not BaseActivity=$activity")
                        }
                    }
                })
    }

    fun showSuccess(tipWord: String? = "", delay: Long = Constant.DelayTime) {
        if (successDialog == null) {
            successDialog = TipDialog.Builder(this.activity)
                .setIconType(TipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord(tipWord)
                .create()
        } else {
            successDialog?.update(tipWord)
        }
        if (successDialog?.isShowing!!) return
        successDialog?.show()
        Observable.timer(delay, TimeUnit.MILLISECONDS)
            .subscribeOn(RxSchedulers.io)
            .observeOn(RxSchedulers.ui)
            .`as`(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
            .subscribe {
                successDialog?.dismiss()
            }
    }

}
